import { Controller } from "../../../../mvc/core/Controller";
import { WindowEvent } from "../../../../component/window/WindowEvent";
import { ThingUtil } from "../../../scene/layer/utils/ThingUtil";
import { PetIslandUtil } from "../PetIslandUtil";
import { PetIslandFightPanel } from "../PetIslandFightPanel";
import { DataEvent } from "../../../events/DataEvent";
import { Game } from "../../../Game";
import { PetIslandFightObserveVo } from "../vo/PetIslandFightObserveVo";
import { GameProxy } from "../../../mvc/GameProxy";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SRobIslandFightingDetail } from "../../../../../Message/Game/SRobIslandFightingDetail";
import { SRobIslandFightResult } from "../../../../../Message/Game/SRobIslandFightResult";
import { PetIslandFightResultItemData } from "../PetIslandFightResultItemData";
import { PetIslandFightResultVo } from "../PetIslandFightResultVo";
import { PetIslandFightResultPanel } from "../PetIslandFightResultPanel";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { IView } from "../../../../mvc/interfaces/IView";
//class PetIslandFightController
    
    export  class PetIslandFightController extends Controller
    {
        constructor()
        {
            super();
            this._attackPetId = [];
            this._defendPetId = [];
            return;
        }

        /* internal  */showPanel(arg1: WindowEvent): void
        {
            if (!this._time) 
            {
                this._time = new Object /* flash.utils.Timer */(1000);
            }
            this._time.addEventListener(flash.events.TimerEvent.TIMER, this.onTimerHandler);
            this._time.start();
            return;
        }

        /* internal  */closePanel(arg1: WindowEvent): void
        {
            this._time.removeEventListener(flash.events.TimerEvent.TIMER, this.onTimerHandler);
            this._time.stop();
            return;
        }

        /* internal  */onTimerHandler(arg1: Object /* flash.events.TimerEvent */=null): void
        {
            var loc1=0;
            var loc2=0;
            var loc4=null;
            var loc3=ThingUtil.sceneEntityUtil.getScene(PetIslandUtil.MAP_ID);
            var loc5=0;
            var loc6=loc3;
            for(loc4 of loc6) 
            {
                if (this._attackPetId.indexOf(loc4.entityInfo.entityInfo.entityId.id) != -1) 
                {
                    loc1 = loc1 + loc4.life;
                }
                if (this._defendPetId.indexOf(loc4.entityInfo.entityInfo.entityId.id) == -1) 
                {
                    continue;
                }
                loc2 = loc2 + loc4.life;
            }
            PetIslandFightPanel(view).updataLife(loc1, loc2);
            return;
        }

        /* internal  */onPetIslandFightObserveHandler(arg1: DataEvent): void
        {
            Game.petIslandFight.petLayer.removeAllChildren();
            var loc1=arg1.data as PetIslandFightObserveVo;
            GameProxy.petIsland.observe(loc1);
            return;
        }

        /* internal  */onPetIslandFightingDetailHandler(arg1: MessageBlock): void
        {
            var loc1=null;
            this._data = arg1.messageBase as SRobIslandFightingDetail;
            this._attackPetId = [];
            this._defendPetId = [];
            var loc2=0;
            var loc3=this._data.attackPlayers;
            for(loc1 of loc3) 
            {
                this._attackPetId.push(loc1.entityId.id);
            }
            loc2 = 0;
            loc3 = this._data.defensePlayers;
            for(loc1 of loc3) 
            {
                this._defendPetId.push(loc1.entityId.id);
            }
            PetIslandFightPanel(view).data = this._data;
            this.onTimerHandler();
            view.show();
            return;
        }

        /* internal  */onPetIslandFightResultHandler(arg1: MessageBlock): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc1=arg1.messageBase as SRobIslandFightResult;
            var loc2=[];
            var loc3=[];
            var loc8=0;
            var loc9=this._data.attackPlayers;
            for(loc4 of loc9) 
            {
                loc5 = ThingUtil.sceneEntityUtil.getEntity(loc4.entityId, PetIslandUtil.MAP_ID);
                (loc6 = new PetIslandFightResultItemData()).guildId = this._data.attackGuildId.id;
                loc6.entityId = loc4.entityId;
                loc6.petName = loc4.petName;
                loc6.playerName = loc4.name;
                loc6.maxLife = loc4.maxLife;
                loc6.life = loc5 ? loc5.entityInfo.entityInfo.life : 0;
                loc2.push(loc6);
            }
            loc8 = 0;
            loc9 = this._data.defensePlayers;
            for(loc4 of loc9) 
            {
                loc5 = ThingUtil.sceneEntityUtil.getEntity(loc4.entityId, PetIslandUtil.MAP_ID);
                (loc6 = new PetIslandFightResultItemData()).guildId = this._data.defenseGuildId.id;
                loc6.entityId = loc4.entityId;
                loc6.petName = loc4.petName;
                loc6.playerName = loc4.name;
                loc6.maxLife = loc4.maxLife;
                loc6.life = loc5 ? loc5.entityInfo.entityInfo.life : 0;
                loc3.push(loc6);
            }
            (loc7 = new PetIslandFightResultVo()).attackPlayers = loc2;
            loc7.defendPlayers = loc3;
            loc7.attackName = this._data.attackGuildName;
            loc7.defendName = this._data.defenseGuildName;
            loc7.attackGuildId = this._data.attackGuildId;
            loc7.defenseGuildId = this._data.defenseGuildId;
            loc7.fightResult = loc1;
            this.resultPanel.data = loc7;
            this.resultPanel.show();
            view.hide();
            return;
        }

        public get resultPanel(): PetIslandFightResultPanel
        {
            if (!this._resultPanel) 
            {
                this._resultPanel = new PetIslandFightResultPanel();
            }
            return this._resultPanel;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PetIslandFightObserve, this.onPetIslandFightObserveHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandFightingDetail, this.onPetIslandFightingDetailHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandFightResult, this.onPetIslandFightResultHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._petIslandFightPanel) 
            {
                this._petIslandFightPanel = new PetIslandFightPanel();
                this._petIslandFightPanel.addEventListener(WindowEvent.SHOW, this.showPanel);
                this._petIslandFightPanel.addEventListener(WindowEvent.CLOSE, this.closePanel);
            }
            return this._petIslandFightPanel;
        }

        /* internal */ /* var */_petIslandFightPanel: PetIslandFightPanel;

        /* internal */ /* var */_resultPanel: PetIslandFightResultPanel;

        /* internal */ /* var */_time: Object /* flash.utils.Timer */;

        /* internal */ /* var */_attackPetId: Array<any>;

        /* internal */ /* var */_defendPetId: Array<any>;

        /* internal */ /* var */_data: SRobIslandFightingDetail;
    }
